package com.bdqn.t362.common.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lqm
 * @ClassName DataUtils
 * @description: TODO
 */
public class DataUtils {
    public static void main(String[] args) {
//        Calendar calendar = Calendar.getInstance();
//        for (int i =1; i<8 ; i++ )
//
//        {
//            calendar = Calendar.getInstance();
//            calendar.set(Calendar.DATE,calendar.get(Calendar.DAY_OF_MONTH)-i);
//            System.out.println(calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH));
//        }

        createDirectory();
    }

    /**
     * 生成结业考答题卡文件夹
     */
    private static void createDirectory() {

        File file = new File("D:\\教学资料\\班级管理\\362\\S2结业考\\复习\\试卷\\笔试");
        List<String> fileNames = new ArrayList<>();
        getFileNames(file, fileNames);

        for (int i = 0; i < fileNames.size(); i++) {
            file = new File("D:\\教学资料\\班级管理\\362\\S2结业考\\复习\\试卷\\笔试" + File.separator + fileNames.get(i));
            if (!file.exists()) {
                file.mkdirs();
            }
        }
    }


    /**
     * 得到文件名称
     *
     * @param file      文件
     * @param fileNames 文件名
     * @return {@link List}<{@link String}>
     */
    private static List<String> getFileNames(File file, List<String> fileNames) {
        File[] files = file.listFiles();
        Set<String> dataSet = new HashSet<>();
        for (File f : files) {
            if (f.isDirectory()) {
                getFileNames(f, fileNames);
            } else {
                String fileName = f.getName();
//                fileNames.add(fileName);
                String[] names = fileName.split("_");

                if (names[0].indexOf("BCSP") != -1) {

                    fileName = names[0].substring(0, names[0].indexOf("BCSP") - 1);
//                    fileName = names[0].substring(0, names[0].indexOf("BCSP") - 1) +
//                            names[names.length - 1].substring(names[names.length - 1].lastIndexOf(".") - 4, names[names.length - 1].lastIndexOf("."));
                } else {
                    fileName = names[0];
//                    fileName = names[0] +
//                            names[names.length - 1].substring(names[names.length - 1].lastIndexOf(".") - 4, names[names.length - 1].lastIndexOf("."));
                }
                if (!dataSet.contains(fileName)) {
                    dataSet.add(fileName);
                    fileNames.add(fileName.replaceAll("-",""));
                    System.out.println(fileName);
                }
            }
        }
        return fileNames;
    }
}
